package com.lg.okframe.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.lg.okframe.R;


import java.util.logging.LogRecord;

/**
 * Created by LG on 2018/8/14.
 */

public class yuan extends View {

    int mRadius = 0;
    int angle = 0;
    myHandler myHandlesr = new myHandler();
    private Thread thread;
    private RectF oval;

    Context context;
    public yuan(@NonNull Context context) {
        super(context);
    }

    public yuan(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
        this.context=context;
    }

    public yuan(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
        this.context=context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(550,550);


    }

    /**
     * @param attrs
     */
    private void init(AttributeSet attrs) {


    }

    Canvas canvas;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;
        int height = getHeight();
        int width = getWidth();

        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#FE671E"));
        paint.setStrokeWidth(150);
        paint.setAntiAlias(true); //消除锯齿
        paint.setStyle(Paint.Style.STROKE); //绘制空心圆
        paint.setStrokeCap(Paint.Cap.ROUND);


        float mR=200;
        RectF oval = new RectF(width/2-mR, height/2-mR, width/2+mR, height/2+mR);
        canvas.drawArc(oval, 0, angle, false, paint);


    }


    public void clear() {

        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPaint(paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        invalidate();
    }

    private static final int START_CIRCLE_ANGLE = 1;
    private static final int END_CIRCLE_ANGLE = 0;
    private static final int SELECT_ANIMATION_DURATION = 100;

    public void startAnmin() {
        thread = new Thread(new myRunne());
        thread.start();
    }


    class myRunne implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    if (angle > 360) {

                        break;
                    }
                    Thread.sleep(2);
                    angle += 3;
                    postInvalidate();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Message message = new Message();
            message.what=1;
            myHandlesr.sendMessage(message);
        }

    }

    private void exitAnmin() {

        //隐藏
        ValueAnimator circleAngleAnimation = ValueAnimator.ofFloat(START_CIRCLE_ANGLE, END_CIRCLE_ANGLE);
        circleAngleAnimation.setDuration(SELECT_ANIMATION_DURATION);
        circleAngleAnimation.setInterpolator(new DecelerateInterpolator());
        circleAngleAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float   currentCircleAngle = (float) animation.getAnimatedValue();
                setAlpha(currentCircleAngle);
            }
        });
        circleAngleAnimation.start();
    }


    class myHandler extends Handler {

//
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    exitAnmin();


                    break;
            }


        }
    }
}
